#include "GameManager.h"
#include <QStandardPaths>
#include <QDir>
#include <QDebug>

GameManager& GameManager::instance()
{
    static GameManager instance;
    return instance;
}

GameManager::GameManager(QObject *parent)
    : QObject(parent)
    , m_settings(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/settings.ini", QSettings::IniFormat)
{
    m_progress.currentLevel = 1;
    loadProgress();
}

void GameManager::saveProgress()
{
    m_progress.lastSaved = QDateTime::currentDateTime();
    
    // 保存当前关卡
    m_settings.setValue("progress/currentLevel", m_progress.currentLevel);
    
    // 保存关卡进度
    m_settings.beginWriteArray("progress/levels");
    int index = 0;
    for (auto it = m_progress.levelProgress.begin(); it != m_progress.levelProgress.end(); ++it) {
        m_settings.setArrayIndex(index++);
        m_settings.setValue("levelId", it.key());
        m_settings.setValue("completed", it.value().completed);
        m_settings.setValue("completionTime", it.value().completionTime);
        m_settings.setValue("attempts", it.value().attempts);
        m_settings.setValue("bestTime", it.value().bestTime);
        m_settings.setValue("lastPlayed", it.value().lastPlayed);
    }
    m_settings.endArray();
    
    m_settings.setValue("progress/lastSaved", m_progress.lastSaved);
    
    m_settings.sync();
    qDebug() << "Game progress saved";
}

void GameManager::loadProgress()
{
    m_progress.currentLevel = m_settings.value("progress/currentLevel", 1).toInt();
    
    // 加载关卡进度
    int size = m_settings.beginReadArray("progress/levels");
    for (int i = 0; i < size; ++i) {
        m_settings.setArrayIndex(i);
        int levelId = m_settings.value("levelId").toInt();
        LevelProgress progress;
        progress.completed = m_settings.value("completed", false).toBool();
        progress.completionTime = m_settings.value("completionTime").toDateTime();
        progress.attempts = m_settings.value("attempts", 0).toInt();
        progress.bestTime = m_settings.value("bestTime", 0).toInt();
        progress.lastPlayed = m_settings.value("lastPlayed").toDateTime();
        
        m_progress.levelProgress[levelId] = progress;
    }
    m_settings.endArray();
    
    m_progress.lastSaved = m_settings.value("progress/lastSaved").toDateTime();
    
    qDebug() << "Game progress loaded, current level:" << m_progress.currentLevel;
}

void GameManager::levelCompleted(int level, int timeTaken)
{
    LevelProgress &progress = m_progress.levelProgress[level];
    progress.completed = true;
    progress.completionTime = QDateTime::currentDateTime();
    progress.attempts++;
    progress.lastPlayed = QDateTime::currentDateTime();
    
    // 更新最佳时间
    if (progress.bestTime == 0 || timeTaken < progress.bestTime) {
        progress.bestTime = timeTaken;
    }
    
    // 自动解锁下一关
    if (level == m_progress.currentLevel) {
        m_progress.currentLevel++;
    }
    
    saveProgress();
    emit levelCompleted(level, timeTaken);
}

void GameManager::levelAttempted(int level)
{
    LevelProgress &progress = m_progress.levelProgress[level];
    progress.attempts++;
    progress.lastPlayed = QDateTime::currentDateTime();
    
    saveProgress();
}

LevelProgress GameManager::getLevelProgress(int level) const
{
    return m_progress.levelProgress.value(level, LevelProgress());
}

bool GameManager::isLevelCompleted(int level) const
{
    return m_progress.levelProgress.value(level).completed;
}

int GameManager::getCompletedLevelCount() const
{
    int count = 0;
    for (const auto &progress : m_progress.levelProgress) {
        if (progress.completed) {
            count++;
        }
    }
    return count;
}

void GameManager::resetProgress()
{
    m_progress.currentLevel = 1;
    m_progress.levelProgress.clear();
    saveProgress();
    qDebug() << "Game progress reset";
}